package com.yunquan.ohana.ui.calendar.task.filter

import android.content.Intent
import android.os.Bundle
import androidx.activity.result.ActivityResult
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.google.gson.Gson
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseActivity
import com.yunquan.ohana.databinding.ActivityTaskFilterBinding
import com.yunquan.ohana.entity.TaskFilterDto
import com.yunquan.ohana.utils.StringUtils
import java.util.Locale

class TaskFilterActivity : BaseActivity<ActivityTaskFilterBinding, TaskFilterViewModel>() {
    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_task_filter
    }

    override fun initVariableId(): Int {
        return BR.taskFilterViewModel
    }

    override fun initParam() {
        super.initParam()
        Locale.setDefault(Locale("en"))
        // 设置背景颜色
        window.statusBarColor = ContextCompat.getColor(this, R.color.c_F6)
        // 设置文字颜色（true = 黑色文字，false = 白色文字）
        val controller = WindowInsetsControllerCompat(window, window.decorView)
        controller.isAppearanceLightStatusBars = true
    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        val bundle = intent.extras
        bundle?.let {
            viewModel!!.mDeviceId = it.getString("deviceId")
            viewModel!!.mUserType = it.getBoolean("userType")
            val dataJson = it.getString("data")
            if (!StringUtils.isEmpty(dataJson)) {
                val bean = Gson().fromJson(dataJson, TaskFilterDto::class.java)
                if (bean != null) {
                    viewModel!!.mFilterBean = bean
                    viewModel!!.completed.set(bean.completed)
                    viewModel!!.lateChores.set(bean.lateChores)
                }
            }
            viewModel!!.getProfilesList()
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel!!.ui.observe(this) { type ->
            if (type == 0) {
                val json = Gson().toJson(viewModel!!.mFilterBean!!)
                val intent = Intent()
                intent.putExtra("data", json)
                setResult(1003, intent)
                finish()
            }
        }
    }

    override fun onActivityResult(result: ActivityResult) {
        super.onActivityResult(result)
        if (result.resultCode == 1001) {
            viewModel!!.getProfilesList()
        }
    }
}